package com.word.Service.Implement;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.word.Service.WordService;
import com.word.entity.word;
import com.word.mapper.WordMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class WordServiceImpl extends ServiceImpl<WordMapper, word> implements WordService {

    @Autowired
    private WordMapper wordMapper;

    @Override
    public void addOrUpdateWord(word word) {
        QueryWrapper<word> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("word", word.getWord());
        word existingWord = wordMapper.selectOne(queryWrapper);

        if (existingWord == null) {
            word.setSearchCount(1);
            wordMapper.insert(word);
        } else {
            existingWord.setSearchCount(existingWord.getSearchCount() + 1);
            wordMapper.updateById(existingWord);
        }
    }
    @Override
    public List<word> getAllWords() {
        QueryWrapper<word> queryWrapper = new QueryWrapper<>();
        return wordMapper.selectList(queryWrapper);
    }
}
